<?php

namespace IDock\Insurance\DBIC\Request;

use IDock\Common\Encrypt\RSAEncrypt;
use IDock\Insurance\DBIC\Response\LoginTokenResponse;
use IDock\Visitor\VehicleModelVisitor;
use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Crypt\RSA;
use phpseclib3\Math\BigInteger;

class LoginTokenRequest extends AllDBICRequest
{
    protected $route = 'dbic/ucs/user/login';

    protected $response = LoginTokenResponse::class;

    protected $urlTip = '获取/刷新token';

    protected $userCode = '';

    protected $passWord;
    protected $module16;
    protected $exponent16;
    protected $passwordRsANoPadding;


    /**
     * @var  VehicleModelVisitor $visitor
     */
    protected $visitor;

    public function generate(): array
    {
        $this->encrypt();

        return [
            'userCode' => $this->userCode,
            'passWord' => $this->passWord
        ];
    }

    /**
     * 密码加密  先RSA加密再BASE64 encode
     * @return void
     * @version 4.0
     */
    private function encrypt()
    {
        //PHP RAS NO PADDING 与JAVA 不一致
        //生成公钥
        //$publicKey = PublicKeyLoader::load([
        //    'n' => new BigInteger($this->module16, 16),
        //    'e' => new BigInteger($this->exponent16, 16),
        //]);
        //
        //$publicKey = RSA::loadPublicKey($publicKey);
        //$publicKey = $publicKey->toString('PKCS8');
        //
        ////公钥加密 用户密码
        //$RSAEncrypt = new RSAEncrypt();
        //$RSAEncrypt->setPublicKey($publicKey);
        //
        //$this->passWord = $RSAEncrypt->publicEncrypt($this->passWord);
        //
        //var_dump($this->passWord);
        //
        //$this->passWord = 'GGb7QIbA2tXLDTrg02LskKF1ZYaPnGYRHzZkXd0L4ypOi8+0zXSOBoD6fpFPX3w9NWYh/41Nx2/F7fq4BWJwyPwzMK35TRak7txZMadgmYbV8X/UMqKlyq1hATBq48CDByZHwWjyalLwNf0MyxDDiYShKWUNc00Cci67aycqQR4=';
        //
        //var_dump($this->passWord);
        $this->passWord = $this->passwordRsANoPadding;
    }

    public function setPassWord($passWord): void
    {
        $this->passWord = $passWord;
    }

    public function setUserCode(string $userCode): void
    {
        $this->userCode = $userCode;
    }

    public function setModule16($module16): void
    {
        $this->module16 = $module16;
    }

    public function setExponent16($exponent16): void
    {
        $this->exponent16 = $exponent16;
    }

    public function setPasswordRsANoPadding($passwordRsANoPadding): void
    {
        $this->passwordRsANoPadding = $passwordRsANoPadding;
    }
}